package smap;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import BD.ProfessorBD;
import DAO.ProfessorDAO;

public class TelaProfessor extends javax.swing.JDialog {
    
    ProfessorBD pro = new ProfessorBD();
    ProfessorDAO proDao = new ProfessorDAO();

    public TelaProfessor(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupoPesquisa = new javax.swing.ButtonGroup();
        pnlVoltar = new javax.swing.JPanel();
        btnVoltar = new javax.swing.JButton();
        pnlTelas = new javax.swing.JPanel();
        btnProfessor = new javax.swing.JButton();
        btnUsuario = new javax.swing.JButton();
        btnCurso = new javax.swing.JButton();
        btnDisciplina = new javax.swing.JButton();
        btnAluno = new javax.swing.JButton();
        btnBackup = new javax.swing.JButton();
        btnTurma = new javax.swing.JButton();
        pnlControle = new javax.swing.JPanel();
        txtPesquisaProfessor = new javax.swing.JTextField();
        cmbPesquisa = new javax.swing.JComboBox();
        btnPesquisar = new javax.swing.JButton();
        btnCarregartodos = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProfessor = new javax.swing.JTable();
        btnExcluir = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();
        btnCadastrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tela Professor");
        setResizable(false);

        pnlVoltar.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));

        btnVoltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/voltar-icon.png"))); // NOI18N
        btnVoltar.setText("Voltar");
        btnVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlVoltarLayout = new javax.swing.GroupLayout(pnlVoltar);
        pnlVoltar.setLayout(pnlVoltarLayout);
        pnlVoltarLayout.setHorizontalGroup(
            pnlVoltarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlVoltarLayout.createSequentialGroup()
                .addGap(528, 528, 528)
                .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlVoltarLayout.setVerticalGroup(
            pnlVoltarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlVoltarLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pnlTelas.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));

        btnProfessor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/professor-icon.png"))); // NOI18N
        btnProfessor.setText("Professor");
        btnProfessor.setEnabled(false);

        btnUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/user-icon.png"))); // NOI18N
        btnUsuario.setText("Usuario");
        btnUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUsuarioActionPerformed(evt);
            }
        });

        btnCurso.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/curso-icon.png"))); // NOI18N
        btnCurso.setText("Curso");
        btnCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCursoActionPerformed(evt);
            }
        });

        btnDisciplina.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/disciplina-icon.png"))); // NOI18N
        btnDisciplina.setText("Disciplina");
        btnDisciplina.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDisciplinaActionPerformed(evt);
            }
        });

        btnAluno.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/aluno-icon.png"))); // NOI18N
        btnAluno.setText("Aluno");
        btnAluno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlunoActionPerformed(evt);
            }
        });

        btnBackup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/backup-icon.png"))); // NOI18N
        btnBackup.setText("Backup");
        btnBackup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackupActionPerformed(evt);
            }
        });

        btnTurma.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/turma-icon.png"))); // NOI18N
        btnTurma.setText("Turma");
        btnTurma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTurmaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlTelasLayout = new javax.swing.GroupLayout(pnlTelas);
        pnlTelas.setLayout(pnlTelasLayout);
        pnlTelasLayout.setHorizontalGroup(
            pnlTelasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlTelasLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnTurma, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnBackup, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );
        pnlTelasLayout.setVerticalGroup(
            pnlTelasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlTelasLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlTelasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnProfessor)
                    .addComponent(btnUsuario)
                    .addComponent(btnAluno)
                    .addComponent(btnBackup)
                    .addComponent(btnCurso, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnDisciplina)
                    .addComponent(btnTurma))
                .addContainerGap())
        );

        pnlControle.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)), "Pesquisar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.darkGray));

        cmbPesquisa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nome", "CPF" }));

        btnPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/procurar-icon.png"))); // NOI18N
        btnPesquisar.setText("Pesquisar");
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });

        btnCarregartodos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/todos-icon.png"))); // NOI18N
        btnCarregartodos.setText("Carregar todos");
        btnCarregartodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCarregartodosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlControleLayout = new javax.swing.GroupLayout(pnlControle);
        pnlControle.setLayout(pnlControleLayout);
        pnlControleLayout.setHorizontalGroup(
            pnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlControleLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(txtPesquisaProfessor)
                .addGap(18, 18, 18)
                .addComponent(btnPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCarregartodos, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pnlControleLayout.setVerticalGroup(
            pnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlControleLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPesquisaProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPesquisar)
                    .addComponent(btnCarregartodos))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblProfessor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));
        tblProfessor.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nome", "CPF", "E-mail"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblProfessor.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblProfessor.getTableHeader().setResizingAllowed(false);
        tblProfessor.getTableHeader().setReorderingAllowed(false);
        tblProfessor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblProfessorMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblProfessor);
        tblProfessor.getColumnModel().getColumn(0).setMinWidth(80);
        tblProfessor.getColumnModel().getColumn(0).setPreferredWidth(80);
        tblProfessor.getColumnModel().getColumn(0).setMaxWidth(100);
        tblProfessor.getColumnModel().getColumn(2).setMinWidth(130);
        tblProfessor.getColumnModel().getColumn(2).setPreferredWidth(130);
        tblProfessor.getColumnModel().getColumn(2).setMaxWidth(160);

        btnExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/delete-icon.png"))); // NOI18N
        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/alterar-icon.png"))); // NOI18N
        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        btnCadastrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/add-icon.png"))); // NOI18N
        btnCadastrar.setText("Cadastrar");
        btnCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCadastrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlTelas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlControle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1)
            .addComponent(pnlVoltar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(398, 398, 398))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnlTelas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlControle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExcluir)
                    .addComponent(btnAlterar)
                    .addComponent(btnCadastrar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void btnVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnVoltarActionPerformed

    private void btnUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUsuarioActionPerformed
        this.dispose();  // Fecha Tela Professor
        TelaUsuario usu = new TelaUsuario(null, true);
        usu.carregarTabela("");
        usu.setVisible(true); //Abre Tela Usuario
    }//GEN-LAST:event_btnUsuarioActionPerformed

    private void btnCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCursoActionPerformed
        this.dispose(); // Fecha Tela Professor
        TelaCurso cur = new TelaCurso(null, true);
        cur.carregarTabela("");
        cur.setVisible(true); // Abre Tela Curso
    }//GEN-LAST:event_btnCursoActionPerformed

    private void btnDisciplinaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDisciplinaActionPerformed
        this.dispose(); // Fecha Tela Professor
        TelaDisciplina dis = new TelaDisciplina(null, true);
        dis.carregarTabela("");
        dis.setVisible(true); // Abre Tela Disciplina
    }//GEN-LAST:event_btnDisciplinaActionPerformed

    private void btnAlunoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlunoActionPerformed
        this.dispose(); // Fecha Tela Professor
        TelaAluno alu = new TelaAluno(null, true);
        alu.carregarTabela("");
        alu.setVisible(true); // Abre Tela Aluno
    }//GEN-LAST:event_btnAlunoActionPerformed

    private void btnBackupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackupActionPerformed
        this.dispose(); // Fecha Tela Professor
        TelaBackup bac = new TelaBackup(null, true);
        bac.setVisible(true); // Abre Tela Backup
    }//GEN-LAST:event_btnBackupActionPerformed

    private void btnCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCadastrarActionPerformed
        this.enable();
        TelaCadProfessor cadprof = new TelaCadProfessor(null,true);
        cadprof.setCadastrar(true);//true para cadastrar
        cadprof.autoIncremento();
        cadprof.setVisible(true);
        //Recarregar a tabela
        carregarTabela("");
    }//GEN-LAST:event_btnCadastrarActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        if(tblProfessor.getSelectedRow() == -1){ // Não selecionou nenhuma Linha
            JOptionPane.showMessageDialog(null,"Selecione um professor para alterar", "Selecione o professor", JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            //Obtem os dados de acordo com a linha ao qual foi selecionada.
            Integer linha = tblProfessor.getSelectedRow();
            pro.setCodigo((Integer)tblProfessor.getValueAt(linha, 0));
            pro.setNome((String)tblProfessor.getValueAt(linha, 1));
            pro.setCpf((String)tblProfessor.getValueAt(linha, 2)); 
            pro.setEmail((String)tblProfessor.getValueAt(linha, 3));
            //Manda os dados para a tela de Alterar
            this.enable();
            TelaCadProfessor altprof = new TelaCadProfessor(null, true);
            altprof.setCadastrar(false); //false para alterar
            altprof.setTitle("Alterar Professor");
            altprof.recebeObjejo(pro);
            altprof.setVisible(true);
            //Recarregar a tabela
            carregarTabela(txtPesquisaProfessor.getText().trim());
        }
        
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        if(tblProfessor.getSelectedRow() == -1){ // Não selecionou nenhuma Linha
            JOptionPane.showMessageDialog(null,"Selecione um professor para excluir", "Selecione o professor", JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            Object[] opcoes = { "Sim", "Não" };
             //Obtem os dados de acordo com a linha ao qual foi selecionada.
            Integer linha = tblProfessor.getSelectedRow();
            Integer resposta =JOptionPane.showOptionDialog(null,"Deseja excluir este professor ?", "Exclusão",JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null,
                    opcoes, opcoes[1]);
            
            // Verifica Resposta do Usuário
            if(resposta == JOptionPane.OK_OPTION){
                //seta o código do professor para exclusão
                pro.setCodigo((Integer)tblProfessor.getValueAt(linha, 0));
                
                //excluir
                if(proDao.excluirProfessor(pro)){
                    JOptionPane.showMessageDialog(null,"Exclusão realizada com sucesso", 
                        "Excluir", JOptionPane.INFORMATION_MESSAGE);   
                    //recarregar o jTable
                    carregarTabela("");
                }
                else{
                    JOptionPane.showMessageDialog(null,"Nao foi possível realizar a exclusão", 
                        "Excluir", JOptionPane.INFORMATION_MESSAGE);
                }
            }
            else{
                JOptionPane.showMessageDialog(null,"Exclusão cancelada", "Exclusão", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed
        carregarTabela(txtPesquisaProfessor.getText().trim());
    }//GEN-LAST:event_btnPesquisarActionPerformed

    private void btnCarregartodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCarregartodosActionPerformed
        carregarTabela("");
    }//GEN-LAST:event_btnCarregartodosActionPerformed

    private void tblProfessorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblProfessorMouseClicked
        if(evt.getClickCount() == 2){
            if (tblProfessor.getSelectedRow() == -1) { // Não selecionou nenhuma Linha
                JOptionPane.showMessageDialog(null, "Selecione um professor para visualizar", "Selecione o professor", JOptionPane.INFORMATION_MESSAGE);
            } else {
                Integer linha = tblProfessor.getSelectedRow();
                //seta os dados do professor
                pro.setCodigo((Integer) tblProfessor.getValueAt(linha, 0));
                pro.setNome((String) tblProfessor.getValueAt(linha, 1));
                pro.setCpf((String) tblProfessor.getValueAt(linha, 2));
                pro.setEmail((String) tblProfessor.getValueAt(linha, 3));
                //Manda os dados para a tela de Alterar
                this.enable();
                TelaCadProfessor visuprof = new TelaCadProfessor(null, true);
                visuprof.visualizaObjeto(pro);
                visuprof.setTitle("Visualizar Professor");
                visuprof.setVisible(true);
            }
        }      
    }//GEN-LAST:event_tblProfessorMouseClicked

    private void btnTurmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTurmaActionPerformed
        this.dispose(); // Fecha Tela Professor
        TelaTurma tur = new TelaTurma(null, true);
        tur.carregarTabela("");
        tur.setVisible(true); // Abre Tela Aluno
    }//GEN-LAST:event_btnTurmaActionPerformed
    
    public void carregarTabela(String valor){
        List<ProfessorBD> listaProfessor = new ArrayList<ProfessorBD>(); 
        
        //Verifica qual opção esta selecionada no Combobox para realizar a pesquisa
        if(cmbPesquisa.getSelectedIndex() == 0){
            listaProfessor = proDao.filtro("nome_prof", valor);
        }else{
                listaProfessor = proDao.filtro("cpf_prof", valor);
        }
        
        //Define o Modelo da JTable
        DefaultTableModel tbm = (DefaultTableModel)tblProfessor.getModel();         
        
        //Remove a Tabela Antiga
        for(int i = tbm.getRowCount()-1; i >= 0; i--){ 
            tbm.removeRow(i); 
        } 
        
        int i = 0; 
        //Preenche a Tabela Nova
        for(ProfessorBD pf : listaProfessor){            
            tbm.addRow(new String[1]); 
            tblProfessor.setValueAt(pf.getCodigo(), i, 0); //Objeto Linha Coluna
            tblProfessor.setValueAt(pf.getNome(), i, 1); 
            tblProfessor.setValueAt(pf.getCpf(), i, 2);
            tblProfessor.setValueAt(pf.getEmail(), i, 3);
            i++; 
        }
        centralizarTable();
    }
    
    public void centralizarTable(){
        DefaultTableCellRenderer center = new DefaultTableCellRenderer();
        center.setHorizontalAlignment(SwingConstants.CENTER ); 
        tblProfessor.getColumnModel().getColumn(0).setCellRenderer(center);
        tblProfessor.getColumnModel().getColumn(2).setCellRenderer(center);    
    }
    
    public static void main(String args[]) {

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaProfessor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaProfessor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaProfessor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaProfessor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TelaProfessor dialog = new TelaProfessor(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnAluno;
    private javax.swing.JButton btnBackup;
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JButton btnCarregartodos;
    private javax.swing.JButton btnCurso;
    private javax.swing.JButton btnDisciplina;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JButton btnProfessor;
    private javax.swing.JButton btnTurma;
    private javax.swing.JButton btnUsuario;
    private javax.swing.JButton btnVoltar;
    private javax.swing.JComboBox cmbPesquisa;
    private javax.swing.ButtonGroup grupoPesquisa;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnlControle;
    private javax.swing.JPanel pnlTelas;
    private javax.swing.JPanel pnlVoltar;
    private javax.swing.JTable tblProfessor;
    private javax.swing.JTextField txtPesquisaProfessor;
    // End of variables declaration//GEN-END:variables
}
